<template>
  <div class="page-register">
    <div class="center-form">
      <h2>注册</h2>
      <!-- 表单 -->
      <el-form :model="ruleForm"
               ref="registerForm"
               :rules="rules"
               label-width="80px">
        <!-- TODO:账号 -->
        <el-form-item label="账号 :"
                      prop="username">
          <el-input type="text"
                    v-model="ruleForm.username"
                    placeholder="输入账号"></el-input>
        </el-form-item>
        <!-- TODO:密码 -->
        <el-form-item label="密码 :"
                      prop="password">
          <el-input type="password"
                    v-model="ruleForm.password"
                    placeholder="输入密码"
                    show-password></el-input>
        </el-form-item>
        <!-- TODO:确认密码 -->
        <el-form-item label="确认 :"
                      prop="checkPass">
          <el-input type="password"
                    v-model="ruleForm.checkPass"
                    placeholder="确认密码"
                    show-password></el-input>
        </el-form-item>
             <!-- TODO:地址 -->
        <el-form-item label="地址 :"
                      prop="city">
          <el-input type="text"
                    v-model="ruleForm.city"
                    placeholder="收货地址"
                    ></el-input>
        </el-form-item>
        <!-- 跳转登录 -->
        <div class="go-login">
          <a @click.prevent="goLoginPage">已有账号，登录</a>
        </div>
        <!-- 注册按钮 -->
        <div class="submit">
          <el-button @click="submitForm('registerForm')">确认注册</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { register } from '@/api/user'
export default {
  data() {
    /* 校验账号->手机号码 */
    const validateUname = (rule, value, callback) => {
      let inpname = /^1[3456789]\d{9}$/
      if (!inpname.test(value)) {
        callback(new Error('输入正确手机号码'))
      } else {
        callback()
      }
    }
    // 校验密码函数
    const validatePass = (rule, value, callback) => {
      let ipupass = /^(?=.*[a-zA-Z])(?=.*\d).{6,20}$/
      if (!ipupass.test(value)) {
        callback(new Error('密码至少包含字母、数字,6~20位'))
      } else {
        callback()
      }
    }
    // 确认密码校验
    const validateCheckPass = (rule, value, callback) => {
      if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      //表单数据
      ruleForm: {
        username: '',
        password: '',
        checkPass: '',
        city:''
      },
      // 校验规则
      rules: {
        username: [
          { required: true, message: '账号不能为空', trigger: 'blur' },
          { validator: validateUname, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { required: true, message: '确认密码不能为空', trigger: 'blur' },
          { validator: validateCheckPass, trigger: 'blur' }
        ],
         city: [
          { required: true, message: '地址不能为空', trigger: 'blur' },
        ]
      }
    }
  },
  methods: {
    // 提交表单处理函数
    submitForm(ruleForm) {
      this.$refs[ruleForm].validate(valid => {
        // 校验通过
        if (valid) {
          let datas = {
            username: this.ruleForm.username,
            password: this.ruleForm.password,
            city:this.ruleForm.city
          }
          register(datas).then(res => {
            this.$message('success', '注册成功')
            this.$router.push('/login')
          })
        } else {
          return false
        }
      })
    },

    // 前往登录页面
    goLoginPage() {
      this.$router.push('/login')
    }
  },
  created() {}
}
</script>

<style lang="less" scoped>
.page-register {
  position: relative;
  /* 页面背景图 */
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/register-bgi.jpg') no-repeat;
  background-size: 100% 100%;
  .center-form {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding-bottom: 30px;
    width: 350px;
    box-sizing: border-box;
    border-radius: 20px;
    background-color: #fff;
    h2 {
      text-align: center;
      margin: 20px 0 25px 0;
      letter-spacing: 20px;
      font-size: 29px;
      text-indent: 20px;
      color: #34c5c5;
    }
    /* 表单 */
    :deep(.el-form-item) {
      margin-bottom: 20px;
      /* label样式修改 */
      .el-form-item__label {
        color: #000;
        position: relative;
        /* *号修改 */
        &::before {
          position: absolute;
          right: 47px;
          top: 1px;
          color: #ff0000;
        }
      }
    }
    /* 表单输入框 */
    :deep(.el-input) {
      width: 85%;
      .el-input__inner {
        width: 230px;
        &:focus {
          border: 1px solid #34c5c5;
        }
      }
    }
    /* 已有账号，跳转到登录页面 */
    .go-login {
      padding-right: 38px;
      text-align: right;
      font-size: 12px;
      a {
        color: #34c5c5;
        cursor: pointer;
      }
    }
    /* 注册 */
    :deep(.submit) {
      margin-top: 25px;
      display: flex;
      justify-content: center;
      .el-button {
        width: 40%;
        padding: 10px 0;
        letter-spacing: 3px;
        font-size: 16px;
        color: #fff;
        cursor: pointer;
        border: none;
        border-radius: 4px;
        background-color: #34c5c5;
        span {
          color: #fff;
        }
      }
    }
  }
}
</style>